#include "translate.h"

translate::translate()
{
    char HEADER_MAGIC[8]={'C','H','A','N','N','E','L','\0'};
    for(int i=0;i<8;i++)
    {
       bin_file.magic[i]=HEADER_MAGIC[i];
    }
}

int translate::convert_bin(MSTAR_TV *ms_tv)
{

    Q_CHECK_PTR(ms_tv);
    this->bin_file.dtv_atv_length=(qint32)ms_tv->count_atv;
     bin_file.flag=25;
    QFile file(STD_FILE_FR_TV);
    if(!file.open(QFile::WriteOnly))
    {
         qDebug("in translate::convert_bin open file error");
         return -1;
    }
    QDataStream out(&file);
    const char *tmp=(const char *)bin_file.magic;
    out<<tmp
            <<bin_file.flag
            <<bin_file.dtv_atv_length;

   // int j=0;
    ushort num=0;
    for(ushort i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
    {
        //j=ms_tv->mstar_atv->ui_ATVPRIndexTable[i];
        //if(ms_tv->mstar_atv->ATVProgramData[j].misc.Unused==1)
         //   num++;
        ushort  val=num;
        out<<val;
        const char *str=(const char *)ms_tv->mstar_atv->ATVProgramData[i].ui_sName;
        QString q_str(str);
        out<<q_str;
        val=ms_tv->mstar_atv->ATVProgramData[i].misc.u_Medium;
        out<<val;
        uint u4_val=(((uint)ms_tv->mstar_atv->ATVProgramData[i].ui2_wPLL)*STEPSIZE)/100;
        out<<u4_val;
        val=(ushort)ms_tv->mstar_atv->ATVProgramData[i].misc.u_Skip;
        out<<val;
        val=(ushort)ms_tv->mstar_atv->ATVProgramData[i].misc.u_Favorite;
        out<<val;
    }
   file.flush();
    MSTAR_DTV_DATA *dtv_data;
    for(uint i=0;i<(uint)ms_tv->mstar_dtv.size();i++)
    {
        dtv_data=ms_tv->mstar_dtv.at(i);
        Q_CHECK_PTR(dtv_data);

        ushort val=dtv_data->ui2_Number;
       // qDebug("%d\t",val);
        out<<val;

        const char *str=(const char *)dtv_data->ui_ServiceName;
        QString name(str);
        out<<name;

        if(i<(int)ms_tv->count_dtvb)
            val=0;
        else
            val=1;
        out<<val;

        val=(ushort)dtv_data->u_ServiceType;
        if(val>4)
            val=5;
        out<<val;

        uint ui4_val=0;
        if(i<ms_tv->count_dtvb)
            ui4_val=(uint)ms_tv->cal_freq(dtv_data->ui2_MuxTableID,true);//频率
        else
            ui4_val=(uint)ms_tv->cal_freq(dtv_data->ui2_MuxTableID,false);
        out<<ui4_val;

        val=(ushort)dtv_data->ui_Favorite;
        out<<val;
        val=(ushort)dtv_data->u_IsSkipped;
        out<<val;
        file.flush();
        dtv_data=NULL;
    }
    file.close();
return 1;
}

int translate::convert_bin(MTK_TV *mt_tv)
{
    Q_CHECK_PTR(mt_tv);
    this->bin_file.dtv_atv_length=(qint32)mt_tv->count_atv;
    bin_file.flag=9;
    QFile file(STD_FILE_FR_TV);
    if(!file.open(QFile::WriteOnly))
    {
         qDebug("in translate::convert_bin open file error");
         return -1;
    }
    QDataStream out(&file);
    const char *tmp=(const char *)bin_file.magic;
    out<<tmp
            <<bin_file.flag
            <<bin_file.dtv_atv_length;
    for(int i=0;i<mt_tv->mtk_atv.size();i++)
    {
        uint num=mt_tv->mtk_atv.at(i)->ui4_ch_val;
        out<<num;
        QString name=mt_tv->mtk_atv.at(i)->ps_name;
        out<<name;
        uint tv_class=(mt_tv->mtk_atv.at(i)->ui4_nw_flag&0x0018)>>3;
        out<<tv_class;
        num=mt_tv->mtk_atv.at(i)->ui4_freq;
        out<<num;
        tv_class=mt_tv->mtk_atv.at(i)->ui4_nw_flag&0x0001;
        out<<tv_class;
        tv_class=mt_tv->mtk_atv.at(i)->ui4_ch_val&0x0002>>1;
        out<<tv_class;
    }
    file.flush();
    for(int i=0;i<mt_tv->mtk_dtv.size();i++)
    {
        uint num=mt_tv->mtk_dtv.at(i)->ui4_ch_val;
        out<<num;
        QString name=mt_tv->mtk_dtv.at(i)->ps_name;
        out<<name;
        uint tv_class=(mt_tv->mtk_dtv.at(i)->ui4_nw_flag&0x0018)>>3;
        out<<tv_class;
        tv_class=mt_tv->mtk_dtv.at(i)->ui2_type;
        out<<tv_class;
        num=mt_tv->mtk_dtv.at(i)->ui4_freq;
        out<<num;
        tv_class=mt_tv->mtk_dtv.at(i)->ui4_nw_flag&0x0001;
        out<<tv_class;
        tv_class=mt_tv->mtk_dtv.at(i)->ui4_nw_flag&0x0002>>1;
        out<<tv_class;
    }
    file.flush();
    file.close();
    return 1;
}

//根据标准数据结构的修改来更新tv端数据结构的修改

int translate::bin_modify(const char *disk,int solution)
{

    if(0==strlen(disk))
        return -1;
    if(1==solution)
    {
        char *tmp1=new char[FILE_PATH_LEN];
        memset(tmp1,0,FILE_PATH_LEN);
        strcpy(tmp1,disk);
        strcat(tmp1,"database\\dtv_cmdb_0.bin");

        if(0!=remove(tmp1))
        {
            qDebug("In translate::bin_modify remove file error");
            return -1;
        }
        char *tmp2=new char[FILE_PATH_LEN];
        memset(tmp2,0,FILE_PATH_LEN);
        strcpy(tmp2,disk);
        strcat(tmp2,"database\\cmdb_0_tmp.bin");
        if(0!=rename(tmp2,tmp1))
        {
            qDebug("In translate::bin_modify rename file error");
            return -1;
        }

        memset(tmp1,0,FILE_PATH_LEN);
        strcpy(tmp1,disk);
        strcat(tmp1,"database\\dtv_cmdb_1.bin");
        if(0!=remove(tmp1))
          {
            qDebug("In translate::bin_modify remove file error");
            return -1;
        }
        memset(tmp2,0,FILE_PATH_LEN);
        strcpy(tmp2,disk);
        strcat(tmp2,"database\\cmdb_1_tmp.bin");

        if(0!=rename(tmp2,tmp1))
        {
            qDebug("In translate::bin_modify rename file error");
            return -1;
        }
        delete tmp1;
        tmp1=NULL;
        delete tmp2;
        tmp2=NULL;
    }else{
        char *tmp1=new char[FILE_PATH_LEN];
        memset(tmp1,0,FILE_PATH_LEN);
        strcpy(tmp1,disk);
        strcat(tmp1,"mtk\\tcl_clone_channellist.bin");

        if(0!=remove(tmp1))
        {
            qDebug("In translate::bin_modify remove file error");
            return -1;
        }
        char *tmp2=new char[FILE_PATH_LEN];
        memset(tmp2,0,FILE_PATH_LEN);
        strcpy(tmp2,disk);
        strcat(tmp2,"mtk\\tmp_mtk_clone.bin");
        if(0!=rename(tmp2,tmp1))
        {
            qDebug("In translate::bin_modify rename file error");
            return -1;
        }

    }
    return 1;

}
